package com.nativetalk.controller.app.three.message;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.message.TdNtMessageInsert;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.controller.app.three.study.ThreeStudyController;
import com.nativetalk.service.three.message.MessageService;
import com.nativetalk.service.three.teacher.search.ThreeTeacherSearchService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeMessageController {
    private static Logger logger = Logger.getLogger(ThreeMessageController.class);
    @Resource
    private MessageService messageService;
    @Resource
    private ThreeTeacherSearchService threeTeacherSearchService;
    /**
     * @return RetInfo
     * @Purpose 学生发消息
     * @version 3.0
     * @time    2016-06-15
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/token/message/send", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo send(@RequestBody TdNtMessageInsert tdNtMessageInsert,
                          @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtMessageInsert));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            retInfo = messageService.sendMembMessage(tdNtMember,1,tdNtMessageInsert,null);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 老师收消息
     * @version 3.0
     * @time    2016-06-15
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/teatoken/message/collect", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo collectTeacher(@RequestBody TdNtMessageInsert tdNtMessageInsert,
                          @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtMessageInsert));

            RetInfo retInfo;
            TdNtTeacherResult tdNtReserveInsert = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo = messageService.sendMembMessage(null,2,tdNtMessageInsert,tdNtReserveInsert);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @return RetInfo
     * @Purpose 教师发消息
     * @version 3.0
     * @time    2016-06-15
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/teatoken/message/send", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo sendTeacher(@RequestBody TdNtMessageInsert tdNtMessageInsert,
                        @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtMessageInsert));

            RetInfo retInfo;
            TdNtTeacherResult tdNtReserveInsert = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo = messageService.collectMembMessage(null,1,tdNtMessageInsert,tdNtReserveInsert);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 学生收消息
     * @version 3.0
     * @time    2016-06-15
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/token/message/collect", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo collect(@RequestBody TdNtMessageInsert tdNtMessageInsert,
                           @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtMessageInsert));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            retInfo = messageService.collectMembMessage(tdNtMember,2,tdNtMessageInsert,null);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 获取老师未读消息总数（包含系统消息数 + 聊天未读的消息数量）(用于显示在tab右上角小红圈里面的数字badge)
     * @version 3.0
     * @time    2016-06-15
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/teatoken/message/size", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo sizeTeacher(@RequestHeader(value = "token", required = false) String token,
                               @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtTeacherResult tdNtReserveInsert = (TdNtTeacherResult) MemcachedUtils.get(token);
            TdNtMessageInsert tdNtMessageInsert = new TdNtMessageInsert();
            tdNtMessageInsert.setCollect_user_id(tdNtReserveInsert.getTea_id());
            tdNtMessageInsert.setUser_type(1);//1 学生发的消息
            retInfo = messageService.findMessageSize(tdNtMessageInsert,version);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 获取学生未读消息总数
     * @version 3.0
     * @time    2016-06-15
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/token/message/size", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo size(@RequestHeader(value = "token", required = false) String token,
                        @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            TdNtMessageInsert tdNtMessageInsert = new TdNtMessageInsert();
            tdNtMessageInsert.setCollect_user_id(tdNtMember.getMemb_id());
            tdNtMessageInsert.setUser_type(2);
            retInfo = messageService.findMessageSize(tdNtMessageInsert,version);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 获取学生未读消息列表
     * @version 3.0
     * @time    2016-06-15
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/token/message/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo list(@RequestBody Map<String, Object> map,
                        @RequestHeader(value = "token", required = false) String token,
                        @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            map.put("user_type",2);
            map.put("collect_user_id",tdNtMember.getMemb_id());
            retInfo = messageService.findMessagePage(map,version);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 获取教师未读消息列表
     * @version 3.0
     * @time    2016-06-15
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/teatoken/message/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo size(@RequestBody Map<String, Object> map,
                        @RequestHeader(value = "token", required = false) String token,
                        @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtTeacherResult tdNtReserveInsert = (TdNtTeacherResult) MemcachedUtils.get(token);
            map.put("user_type",1);
            map.put("collect_user_id",tdNtReserveInsert.getTea_id());
            retInfo = messageService.findMessagePage(map,version);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 获取学生未读系统消息列表
     * @version 3.2
     * @time    2016-08-27
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/token/system/message/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo system(@RequestBody Page page,
                          @RequestHeader(value = "token", required = false) String token,
                          @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            TdNtMember tdNtMember = (TdNtMember) MemcachedUtils.get(token);
            page.setObj(tdNtMember.getMemb_id());
            page.setDict_id(1);
            retInfo = messageService.findSystemMessage(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 获取教师未读系统消息列表
     * @version 3.2
     * @time    2016-08-27
     * @author lizhun
     */
    @RequestMapping(value = "/app/8/teatoken/system/message/list", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo systemsize(@RequestBody Page page,
                              @RequestHeader(value = "token", required = false) String token,
                              @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(page));

            RetInfo retInfo;
            TdNtTeacherResult tdNtReserveInsert = (TdNtTeacherResult) MemcachedUtils.get(token);
            page.setObj(tdNtReserveInsert.getTea_id());
            page.setDict_id(2);
            retInfo = messageService.findSystemMessage(page);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
    /**
     * @return RetInfo
     * @Purpose 聊天时获取相关信息
     * @version 3.3
     * @time    2016-09-27
     * @author lizhun
     */
    @RequestMapping(value = "/app/9/message/teacher", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo teacher(@RequestBody Map<String, Integer> map,
                              @RequestHeader(value = "token", required = false) String token,
                              @RequestHeader(value = "version", required = false) Integer version) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            retInfo = threeTeacherSearchService.findTeacher(map.get("tea_id"));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }
}



